package com.ellricky.baselib.adapter.course

import android.app.Activity
import android.view.View
import android.widget.CheckBox
import android.widget.FrameLayout
import android.widget.TextView
import com.andr.common.ui.adapter.BaseRecyclerAdapter
import com.andr.common.ui.adapter.SmartViewHolder
import com.andr.common.util.StrUtil
import com.andr.common.util.ViewUtil
import com.ellricky.baselib.R
import com.ellricky.baselib.entity.course.FileLearnEntity
import com.ellricky.baselib.listener.OnChooseSingleListener

class CourseFileLearnAdapter(private val act: Activity, val canChoose: Boolean, var choosePos: Int = -1, val listener: OnChooseSingleListener? = null) :
        BaseRecyclerAdapter<FileLearnEntity>(R.layout.base_adapter_course_file_learn) {


    override fun onBindViewHolder(vh: SmartViewHolder?, p1: FileLearnEntity?, position: Int) {
        vh?.let {
            val tvAgeRange = it.itemView.findViewById<TextView>(R.id.tvAgeRange)
            tvAgeRange.visibility = View.VISIBLE
            it.text(R.id.tvKey, act.getString(R.string.base_key_course_type))
            it.text(R.id.tvDateKey, act.getString(R.string.base_online_date))

            it.text(R.id.tvTitle, p1?.title)
            tvAgeRange.text = act.getString(R.string.base_age_range, StrUtil.filterEmpty(p1?.ageName))
            it.text(R.id.tvValue, p1?.classTypeName)
            it.text(R.id.tvDate, p1?.pubDate)

            when (canChoose) {
                true -> {
                    vh.gone(R.id.spaceL)
                    vh.gone(R.id.spaceR)

                    val flChoose = it.itemView.findViewById<FrameLayout>(R.id.flChoose)
                    flChoose.visibility = View.VISIBLE

                    val cb = it.itemView.findViewById<CheckBox>(R.id.cb)
                    cb.isChecked = choosePos == position
                }
                false -> {
                    vh.visible(R.id.spaceL)
                    vh.visible(R.id.spaceR)
                    vh.gone(R.id.flChoose)
                }
            }
        }
    }
}